package me.zhengjie.modules.forecast.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.heshuo.amm.exception.DatabaseException;
import com.heshuo.amm.model.METStation;
import com.heshuo.amm.model.ReplaceAirport;
import com.heshuo.amm.model.SimpleLanguage;
import com.heshuo.amm.model.SystemConfig;
import com.heshuo.amm.service.IMETAuthorityService;
import com.heshuo.amm.service.IReplaceAirportService;
import com.heshuo.amm.service.ISimpleLanguageService;
import com.heshuo.amm.service.ISystemConfigService;


@RestController
@RequestMapping("api")
public class ConfigController {

	@Autowired
	ISystemConfigService systemConfigService;
	@Autowired
	IReplaceAirportService replaceAirportService;
	@Autowired
	ISimpleLanguageService simpleLanguageService;
	@Autowired
	IMETAuthorityService mETAuthorityService; 
	
	
    @GetMapping(value = "/config/getAFTNConfig")
    public ResponseEntity<SystemConfig> getAFTNConfig(){
    	SystemConfig sc = systemConfigService.getSystemConfig(null, SystemConfig.AFTN);
    	return new ResponseEntity<SystemConfig>(sc, HttpStatus.OK);
    }
    @GetMapping(value = "/config/getLocalConfig")
    public ResponseEntity<SystemConfig> getLocalConfig(){
    	SystemConfig sc = systemConfigService.getSystemConfig(null, SystemConfig.LOCAL_CONFIG);
    	return new ResponseEntity<SystemConfig>(sc, HttpStatus.OK);
    }
    @GetMapping(value = "/config/getMessageConfig")
    public ResponseEntity<SystemConfig> getMessageConfig(){
    	SystemConfig sc = systemConfigService.getSystemConfig(null, SystemConfig.FORECAST);
    	return new ResponseEntity<SystemConfig>(sc, HttpStatus.OK);
    }
    @GetMapping(value = "/config/getCheckConfig")
    public ResponseEntity<SystemConfig> getCheckConfig(){
    	SystemConfig sc = systemConfigService.getSystemConfig(null, SystemConfig.PUBLISH_CHECK);
    	return new ResponseEntity<SystemConfig>(sc, HttpStatus.OK);
    }
    @GetMapping(value = "/config/getSendMessageAlertConfig")
    public ResponseEntity<SystemConfig> getSendMessageAlertConfig(){
    	SystemConfig sc = systemConfigService.getSystemConfig(null, SystemConfig.SEND_MESSAGE_ALERT);
    	return new ResponseEntity<SystemConfig>(sc, HttpStatus.OK);
    }

    @PostMapping(value = "/config/saveConfig")
    public ResponseEntity<Boolean> saveConfig(@RequestBody SystemConfig vmsg){
    	systemConfigService.saveSystemConfig(vmsg);
        return new ResponseEntity<Boolean>(true, HttpStatus.OK);
    }
    

    @GetMapping(value = "/config/agentAirportList")
//    public ResponseEntity agentAirportList(DictDTO resources, Pageable pageable){
    public ResponseEntity agentAirportList() throws DatabaseException{
        	List list = replaceAirportService.listAll();
        	Map result = new HashMap();
        	result.put("content", list);
        	result.put("totalElements", list.size());
			return new ResponseEntity(result,HttpStatus.OK);
    }


    @GetMapping(value = "/config/simpleLanguageList")
    public ResponseEntity simpleLanguageList(SimpleLanguage simpleLanguage) throws DatabaseException{
//        	List list = simpleLanguageService.findByParam(simpleLanguage.getType(), simpleLanguage.getText(), simpleLanguage.getInterpretation());
    		List list = simpleLanguageService.findAll();        	
        	Map result = new HashMap();
        	result.put("content", list);
        	result.put("totalElements", list.size());
			return new ResponseEntity(result,HttpStatus.OK);
    }


    @GetMapping(value = "/config/airportList")
    public ResponseEntity airportList() throws DatabaseException{
    		List<METStation> list = mETAuthorityService.listAllMETStation();       	
        	Map result = new HashMap();
        	for(METStation ms : list){
        		ms.getRegionalMETCenter().setMetStations(null);
        		ms.getRegionalMETCenter().setMetCenter(null);
        		//以下if,适应二连浩特为空的情况。
        		if(null==ms.getCloudPs())ms.setCloudPs("60,150,450,1500");
        		if(null==ms.getTrendCloudPs())ms.setTrendCloudPs("30,60,150,450");
        		if(null==ms.getVisP())ms.setVisPs("600,800,1500,3000");
        		if(null==ms.getTrendVisPs())ms.setTrendVisPs("150,350,600,800,1500,3000");
        	}
        	result.put("content", list);
        	result.put("totalElements", list.size());
			return new ResponseEntity(result,HttpStatus.OK);
    }
    
}
